 package app.Insam.VNC;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.widget.Toast;

public class InsamPreferences extends PreferenceActivity implements OnPreferenceChangeListener{	
	public static final String TAG = "InsamApp";
	
	public static boolean ChangeUse;	
	SharedPreferences mPref;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);	
	    
	    addPreferencesFromResource(R.xml.setting);
	    mPref = PreferenceManager.getDefaultSharedPreferences(this);
	    
	    ChangeUse=false;
	    
	    SetAutoSummary(InsamPreferencesKey.ColorFormat);
	    SetAutoSummary(InsamPreferencesKey.HomePC_IP);
	    SetAutoSummary(InsamPreferencesKey.HomePC_Port);
	    SetAutoSummary(InsamPreferencesKey.OfficePC_IP);
	    SetAutoSummary(InsamPreferencesKey.OfficePC_Port);
	    SetAutoSummary(InsamPreferencesKey.OfficePC_ID);
	    findPreference(InsamPreferencesKey.HomePC_Use.toString()).setOnPreferenceChangeListener(this);
	    findPreference(InsamPreferencesKey.OfficePC_Use.toString()).setOnPreferenceChangeListener(this);

	    /* below is for debug */	   
	    SetAutoSummary(InsamPreferencesKey.Office_SSID);
	    SetAutoSummary(InsamPreferencesKey.Office_Location);
		findPreference("DBG").setOnPreferenceChangeListener(this);
	}
	
	protected void SetAutoSummary(InsamPreferencesKey _key) {		
		String strKey = _key.toString();
	    String value = mPref.getString(strKey, null);	
	    if(value==null)	return;
	    
		Preference pf = findPreference(strKey);	    
		pf.setSummary(value);
		
		switch(_key) {
		case HomePC_IP:
		case OfficePC_IP:			
		case Office_Location:
			((EditTextPreference)pf).getEditText().setRawInputType(InputType.TYPE_CLASS_NUMBER);
			break;		
		}
		
		pf.setOnPreferenceChangeListener(this);
	}

	public boolean onPreferenceChange(Preference _pref, Object _value) {
	
		switch(InsamPreferencesKey.keyOf(_pref.getKey())) {
		case GPS_Use:
			break;
		case ColorFormat:
			_pref.setSummary((String)_value);
			break;
		case OfficePC_Use:	
			if((Boolean)_value) {
				startService(new Intent(this, InsamService.class).addFlags(InsamService.ACTION_SCAN_LOCATION));
			} else {
				stopService(new Intent(this, InsamService.class));
				InsamDebugger.cancel();
			}			
		case HomePC_Use:
			ChangeUse=true;
			break;	
		case HomePC_IP:
			if(((String)_value).matches("([0-9]{1,3}\\.){3}[0-9]{1,3}")) {
				if(getIntent().getIntExtra("extra", 0)==InsamMain.EXTRA_HOME) {
					setResult(RESULT_OK);
				}
				_pref.setSummary((String)_value);		
				break;
			} else {
				Toast.makeText(this, R.string.text_Reject_input, Toast.LENGTH_LONG).show();
				return false;
			}		
		case HomePC_Port:	
			_pref.setSummary((String)_value);
			break;
//		case HomePC_KeepPasswd:
//			break;
		case OfficePC_IP:
			if(((String)_value).matches("([0-9]{1,3}\\.){3}[0-9]{1,3}")) {
				if(getIntent().getIntExtra("extra", 0)==InsamMain.EXTRA_OFFICE) {
					setResult(RESULT_OK);
				}
				_pref.setSummary((String)_value);		
				break;
			} else {
				Toast.makeText(this, R.string.text_Reject_input, Toast.LENGTH_LONG).show();
				return false;
			}			
		case OfficePC_Port:
			_pref.setSummary((String)_value);
			break;
		case OfficePC_ID:
			if(((String)_value).length()<4) {
				Toast.makeText(this, R.string.text_Reject_input, Toast.LENGTH_LONG).show();
				return false;				
			}
			_pref.setSummary((String)_value);
			break;
//		case OfficePC_AutoLogin:
//			break;
		
		/* followings are for debugging */
		case Debugger_Use:		
			InsamDebugger.set((Boolean)_value);
			break;
		case Office_SSID:
			_pref.setSummary((String)_value);
			InsamApp.OfficeSSID = (String)_value;
			startService(new Intent(this, InsamService.class).addFlags(InsamService.ACTION_SCAN_WIFI));
			break;
		case Office_Location:
			_pref.setSummary((String)_value);
			InsamApp.OfficeLocation = new InsamLocationInfo((String)_value);
			startService(new Intent(this, InsamService.class).addFlags(InsamService.ACTION_SCAN_LOCATION));
			break;
		}
		return true;
	}
}
